<script setup lang="ts">
import type { FormRules } from 'element-plus'
import { onMounted, reactive, ref } from 'vue'

interface Props {
  data?: Record<string, any> | never | null
  status?: string | never | null
}

const { data = null } = defineProps<Props>()

const formRef = ref()

const form = reactive<Record<string, any>>({})

const rules = reactive<FormRules>({
  name: [{ required: true, message: '名称不能为空!', trigger: 'blur' }],
})

const initForm = () => {
  if (data) {
    Object.keys(data).forEach((key: string) => {
      form[key] = data![key]
    })
  }
}

const formatForm = () => {
  const result: Record<string, any> = {}
  Object.keys(form).forEach((key: string) => {
    result[key] = form[key]
  })
  return result
}

const submitForm = () => {
  return new Promise<any>((resolve) => {
    formRef.value.validate((valid: boolean) => {
      let res = null
      if (valid) {
        res = formatForm()
      }
      resolve(res)
    })
  })
}

defineExpose({
  submitForm,
})

onMounted(() => {
  initForm()
})
</script>

<template>
  <div class="form-wrapper">
    <BaseForm
      ref="formRef"
      :form="form"
      :rules="rules"
      width="360px"
      :attributes="{ 'label-width': '90px' }"
    >
      
      <el-form-item label="名称:" prop="name">
        <el-input v-model="form.name" type="text" placeholder="请输入内容"> </el-input>
      </el-form-item>
    </BaseForm>
  </div>
</template>

<style lang="scss" scoped></style>
